package com.wjc.myktapplication.page.wxaccount

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.wjc.basemodule.base.BaseFragment
import com.wjc.basemodule.base.DataBindingConfig
import com.wjc.basemodule.utils.LogTools
import com.wjc.myktapplication.BR
import com.wjc.myktapplication.R
import com.wjc.myktapplication.databinding.FragmentAccountMainBinding

/**
 * 公众号页面
 */
class AccountMainFragment : BaseFragment<FragmentAccountMainBinding>(), View.OnClickListener {
    val TAG = "AccountMainFragment"
    private lateinit var accountVm: AccountViewModel
    private var tabLayoutMediator: TabLayoutMediator? = null
    override val dataBindingConfig: DataBindingConfig
        get() = DataBindingConfig(R.layout.fragment_account_main)

    override fun initViewModel() {
        accountVm = getFragmentViewModel(AccountViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        accountVm.fetchWxAccountList()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        accountVm.wxAccountList.observe(viewLifecycleOwner) {
            LogTools.d(TAG, ":onViewCreated --lifecycle=${this.lifecycle}," + "viewLifecycleOwner.lifecycle=${this.viewLifecycleOwner.lifecycle} it.isSuccess()=")
            if (it != null && it.isSuccess()) {
                defaultPage?.hideLoading()
                defaultPage?.hideDefaultPage()
                tabLayoutMediator = TabLayoutMediator(binding.tabs, binding.viewPager, true) { tab: TabLayout.Tab, position: Int ->
                    tab.text = it.data?.get(position)?.name
                }

                binding.viewPager.adapter = object : FragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle) {
                    override fun createFragment(position: Int): Fragment {
                        LogTools.d(TAG, ":id ---${it.data?.get(position)?.courseId} --pos=$position ")
                        return WxArtFragment.newInstance(position.toString(), it.data?.get(position)?.id)
                    }

                    override fun getItemCount(): Int {
                        return it.data?.size ?: 0
                    }
                }
                tabLayoutMediator?.attach()
            } else {
                defaultPage?.showNetworkErrorContent {
                    run {
                        defaultPage?.startLoading()
                        accountVm.fetchWxAccountList()
                    }
                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        tabLayoutMediator?.detach()
//        LogTools.d(TAG, ":onDestroyView ")
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.iv_room_edit_back -> {}
            else -> {}
        }
    }

}